package c2.cross.business.minecraft.core;

import c2.cross.business.base.CrossSubModule;
import c2.cross.business.minecraft.MinecraftModule;
import cate.common.game.mail.MailCmd;
import cate.game.pvp.minecraft.event.MinecraftNotificationEvent;
import cate.game.pvp.minecraft.event.MinecraftStatusEvent;
import cate.game.pvp.minecraft.MinecraftInnerMsg;
import cate.game.pvp.minecraft.MinecraftMessagingModule.*;
import cate.game.res.MixRes;
import cate.game.role.pvp.minecraft.msg.*;
import easy.java.dev.note.NoteClass;
import easy.java.practice.errorcode.EcResult;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@NoteClass("灵能矿洞-消息分发模块")
public class MinecraftCrossMessagingModule extends CrossSubModule<MinecraftModule> {

    /**
     * 同步玩法信息事件
     *
     * @param event 事件
     */
    public void dispatchStatusEvent(MinecraftStatusEvent event) {
        try {
            useRpcSend().fireAndForgetGameInCrossTemplate(MinecraftInnerMsg.MC_STATUS_EVENT,
                    parent.code(),
                    event);
        } catch (Exception e) {
            log.error("", e);
        }
    }

    /**
     * 同步矿点改变事件
     *
     * @param event 事件
     */
    public void dispatchNotificationEvent(MinecraftNotificationEvent event) {
        try {
            useRpcSend().fireAndForgetGameInCrossTemplate(MinecraftInnerMsg.MC_NOTIFICATION_EVENT,
                    parent.code(),
                    event);
        } catch (Exception e) {
            log.error("", e);
        }
    }

    /**
     * 同步矿区状态
     *
     * @param srvId     服务器ID
     * @param close     是否关闭
     * @param openTime  开启时间
     * @param closeTime 结束时间
     */
    public void sendBackMineAreaState(int srvId, boolean close, long openTime, long closeTime) {
        try {
            useRpcSend().fireAndForgetGameTemplate(MinecraftInnerMsg.MC_GAME_CONNECT_BACK,
                    srvId,
                    CrossMineAreaState.of(close, openTime, closeTime));
        } catch (Exception e) {
            log.error("", e);
        }
    }

    /**
     * 同步用户占用信息
     *
     * @param info 占用信息
     */
    public void syncRoleUsedInfo(CrossRoleUsedInfo info) {
        try {
            useRpcSend().fireAndForgetGameTemplate(MinecraftInnerMsg.MC_SYNC_USED_INFO,
                    info.srvId,
                    info);
        } catch (Exception e) {
            log.error("", e);
        }
    }

    /**
     * 回复查看概览
     *
     * @param roleUid 角色UID
     * @param srvId   服务器ID
     * @param resp    返回包
     */
    public void sendBackOverview(String roleUid, int srvId, MinecraftMineAreaOverviewResp resp) {
        try {
            useRpcSend().fireAndForgetGameTemplate(MinecraftInnerMsg.MC_OVERVIEW_BACK,
                    srvId,
                    CrossOverviewBack.of(roleUid, srvId, resp));
        } catch (Exception e) {
            log.error("", e);
        }
    }

    /**
     * 回复查看层信息
     *
     * @param roleUid 角色UID
     * @param srvId   服务器ID
     * @param result  返回结果
     */
    public void sendBackViewLayer(String roleUid, int srvId, EcResult<MinecraftMineAreaLayerInfoResp> result) {
        try {
            useRpcSend().fireAndForgetGameTemplate(MinecraftInnerMsg.MC_VIEW_LAYER_BACK,
                    srvId,
                    CrossViewLayerBack.of(roleUid, srvId, result));
        } catch (Exception e) {
            log.error("", e);
        }
    }

    /**
     * 回复查看我的点位信息
     *
     * @param roleUid 角色UID
     * @param srvId   服务器ID
     * @param resp    回复
     */
    public void sendBackViewMyPoints(String roleUid, int srvId, MinecraftMineAreaMyPointsResp resp) {
        try {
            useRpcSend().fireAndForgetGameTemplate(MinecraftInnerMsg.MC_VIEW_MY_POINTS_BACK,
                    srvId,
                    CrossViewMyPointsBack.of(roleUid, srvId, resp));
        } catch (Exception e) {
            log.error("", e);
        }
    }

    /**
     * 回复查看点位详情
     *
     * @param roleUid 角色UID
     * @param srvId   服务器ID
     * @param r       结果
     */
    public void sendBackViewPointDetail(String roleUid, int srvId, EcResult<MinecraftMineAreaPointDetailResp> r) {
        try {
            useRpcSend().fireAndForgetGameTemplate(MinecraftInnerMsg.MC_VIEW_POINT_DETAIL_BACK,
                    srvId,
                    CrossViewPointDetailBack.of(roleUid, srvId, r));
        } catch (Exception e) {
            log.error("", e);
        }
    }

    /**
     * 回复游戏服尝试战斗
     *
     * @param back 回复信息
     */
    public void sendBackTryFight(CrossTryFightPointBack back) {
        try {
            useRpcSend().fireAndForgetGameTemplate(MinecraftInnerMsg.MC_TRY_FIGHT_BACK,
                    back.srvId,
                    back);
        } catch (Exception e) {
            log.error("", e);
        }
    }

    /**
     * 回复游戏服战斗结果
     *
     * @param roleUid 角色UID
     * @param srvId   服务器ID
     * @param result  返回结果
     */
    public void sendBackFightResult(String roleUid,
                                    int srvId,
                                    byte fightType,
                                    MixRes lostRes,
                                    long protectTime,
                                    EcResult<MinecraftMineAreaFightResultResp> result) {
        try {
            useRpcSend().fireAndForgetGameTemplate(MinecraftInnerMsg.MC_FIGHT_RESULT_REPORT_BACK,
                    srvId,
                    CrossFightResultReportBack.of(roleUid, srvId, fightType, lostRes, protectTime, result));
        } catch (Exception e) {
            log.error("", e);
        }
    }

    /**
     * 回复领取挂机奖励
     *
     * @param roleUid 角色UID
     * @param srvId   服务器ID
     * @param result  返回结果
     */
    public void sendBackGetHangReward(String roleUid, int srvId, EcResult<MixRes> result) {
        try {
            useRpcSend().fireAndForgetGameTemplate(MinecraftInnerMsg.MC_GET_HANG_REWARD_BACK,
                    srvId,
                    CrossGetHangRewardBack.of(roleUid, srvId, result));
        } catch (Exception e) {
            log.error("", e);
        }
    }

    /**
     * 回复保护矿点
     *
     * @param roleUid 角色UID
     * @param srvId   服务器ID
     * @param result  返回结果
     */
    public void sendBackProtectPoint(String roleUid, int srvId, EcResult<Long> result) {
        try {
            useRpcSend().fireAndForgetGameTemplate(MinecraftInnerMsg.MC_PROTECT_POINT_BACK,
                    srvId,
                    CrossProtectPointBack.of(roleUid, srvId, result));
        } catch (Exception e) {
            log.error("", e);
        }
    }

    /**
     * 回复放弃矿点
     *
     * @param roleUid 角色UID
     * @param srvId   服务器ID
     * @param layer   层数
     * @param tid     配置ID
     * @param result  返回结果
     */
    public void sendBackGiveUpPoint(String roleUid, int srvId, int layer, int tid, EcResult<MixRes> result) {
        try {
            useRpcSend().fireAndForgetGameTemplate(MinecraftInnerMsg.MC_GIVE_UP_POINT_BACK,
                    srvId,
                    CrossGiveUpPointBack.of(roleUid, srvId, layer, tid, result));
        } catch (Exception e) {
            log.error("", e);
        }
    }

    /**
     * 通知游戏服添加邮件
     *
     * @param roleUid 角色UID
     * @param srvId   服务器ID
     * @param mail    邮件
     */
    public void sendAddMail(String roleUid, int srvId, MailCmd mail) {
        mail.addRole(roleUid).withSrvId(srvId);
        context.common.sendMail(mail);
    }
}
